package com.jd.jstable.strategy.jim;

import com.jd.jim.cli.Cluster;
import com.jd.jim.cli.ReloadableJimClientFactory;
import com.jd.jim.cli.ReloadableJimClientFactoryBean;
import com.jd.jim.cli.config.client.ConfigClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by chenyipu on 2018/6/6.
 */
@Service("jstable_______JimClient")
public class JStableReloadableJimClientFactoryBean extends ReloadableJimClientFactory implements FactoryBean, DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(ReloadableJimClientFactoryBean.class);

    @Value("${jstable.jimdb.url}")
    String url;

    @Autowired
    @Qualifier("jstable_______JimConfigClient")
    ConfigClient configClient;
    public JStableReloadableJimClientFactoryBean() throws Exception {
    }

    public Object getObject() throws Exception {
        this.setJimUrl(url);
        this.setConfigClient(configClient);
        return this.getClient();
    }

    public Class<?> getObjectType() {
        return Cluster.class;
    }

    public boolean isSingleton() {
        return true;
    }

    public void destroy() throws Exception {
        logger.info("Method[com.jd.jim.cli.ReloadableJimClientFactoryBean.destroy()] is called.");
        super.clear();
    }
}
